﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using System.Text;
using Common;

public partial class community_topiclist : MemberPageBase
{
    public string ColumnName = "";
    protected void Page_Load(object sender, EventArgs e)
    {
        Response.Expires = -1;

        switch (RequestBase.GetFormString("otype"))
        {
            case "savecomment":
                SaveComment();
                break;
            case "loadingcommentsitems":
                LoadingCommentsItems();
                break; 
        }

        if (!IsPostBack)
        {
            if (string.IsNullOrEmpty(UserName))
            {
                txtLoginState.Value = "0";
                panelCommentEditor.Style.Add("display", "none");
            }
            else
            {
                txtLoginState.Value = "1";
                txtAuthor.Value = this.UserName;
            }
            txtColumnId.Value = Utils.IsNumber(RequestBase.GetQueryString("cid")) ? RequestBase.GetQueryString("cid") : "1";

            Model.BBS.BBSColumnInfo _obj = (new BLL.BBS.BBSBO()).GetBBSColumn(int.Parse(txtColumnId.Value));
            ColumnName = _obj.Name;
        }
    }

    #region 保存评论
    Model.BBS.BBSInfo BBSObj
    {
        get
        {
            Model.BBS.BBSInfo _obj = new Model.BBS.BBSInfo();

            _obj.Title = RequestBase.GetFormString("title");
            _obj.Content = RequestBase.GetFormString("comments");
            _obj.UserNickName = RequestBase.GetFormString("author");
            _obj.UserID = this.UserId; 
            _obj.PublishTime = DateTime.Now.ToString();
            _obj.LastPostTime = DateTime.Now;
            _obj.ColumnID = int.Parse(string.IsNullOrEmpty(RequestBase.GetFormString("columnid")) ? "0" : RequestBase.GetFormString("columnid"));

            return _obj;
        }
    }

    void SaveComment()
    {
        if (!string.IsNullOrEmpty(this.UserName))
        {
            bool bl = (new BLL.BBS.BBSBO()).SaveBBS(BBSObj);

            if (bl)
                BLL.AjaxHelper.ResponseWrite("succeeded");
            else
                BLL.AjaxHelper.ResponseWrite("error.");
        }
        else
            BLL.AjaxHelper.ResponseWrite("unlogin");
    }
    #endregion

    #region 评论列表
    void LoadingCommentsItems()
    {
        try
        {
            string ctrl = "~/_controls/ctlComments.ascx";
            Control uc_rep = LoadControl(ctrl);
            Repeater rptCommentsList = (Repeater)uc_rep.FindControl("rptCommentsList");

            StringBuilder SQL = new StringBuilder();
            SQL.Append(" AND ColumnID = " + (Utils.IsNumber(RequestBase.GetFormString("columnid")) ? RequestBase.GetFormString("columnid") : "1"));
            if (RequestBase.GetFormString("querytext") != "" && !FilterHandler.ExistBadWord(RequestBase.GetFormString("querytext")) && Utils.IsSafeSqlString(RequestBase.GetFormString("querytext")))
            {
                SQL.Append(" AND (Title LIKE '%" + RequestBase.GetFormString("querytext") + "%' OR [Content] LIKE '%" + RequestBase.GetFormString("querytext") + "%' OR UserNickName LIKE '%" + RequestBase.GetFormString("querytext") + "%')");
            }

            DBUtility.Pagination pagination = new DBUtility.Pagination();
            /// 页码
            if (RequestBase.GetFormString("page") != "" && Utils.IsNumber(RequestBase.GetFormString("page")))
                pagination.Sequence = Convert.ToInt32(RequestBase.GetFormString("page"));
            else
                pagination.Sequence = 1;

            if (RequestBase.GetFormString("upcount") != "" && Utils.IsNumber(RequestBase.GetFormString("upcount")))
                pagination.PageUpCount = Convert.ToInt32(RequestBase.GetFormString("upcount"));

            if (RequestBase.GetFormString("direction") == "next")
                pagination.Direction = "next";
            else
                pagination.Direction = "prev";

            pagination.PageBarCount = 8;
            pagination.PageSize = 20;
            pagination.Primarykey = "ID";
            pagination.SearchField = "*";
            pagination.TableName = "BBS";
            pagination.ItemUnit = "条记录";
            pagination.SearchSentence = SQL.ToString();
            pagination.SortSentence = " ORDER BY LastPostTime DESC";

            rptCommentsList.DataSource = pagination.GetDataTable();
            rptCommentsList.DataBind();

            pagination.GoNumeberPageBarCn4();
            uc_rep.GetType().GetProperty("PaginationBar").SetValue(uc_rep, pagination.PageBar, null);

            StringBuilder strb = new StringBuilder();
            StringWriter sw = new StringWriter(strb);
            HtmlTextWriter htw = new HtmlTextWriter(sw);
            rptCommentsList.RenderControl(htw);

            BLL.AjaxHelper.ResponseWrite(strb.ToString());
        }
        catch (System.Threading.ThreadAbortException err)
        {
            //nothing   to   do   
        }
        catch (Exception err)
        {
            BLL.AjaxHelper.ResponseWrite(err.Message);
        }
    }
    #endregion
}